package cn.shop.cloud.service.impl;


import cn.shop.cloud.common.BusinessException;
import cn.shop.cloud.domain.OrderInfo;
import cn.shop.cloud.mapper.OrderInfoMapper;
import cn.shop.cloud.service.IOrderInfoService;
import cn.shop.cloud.service.ISeckillGoodService;
import cn.shop.cloud.service.ISeckillOrderService;
import cn.shop.cloud.web.msg.SeckillCodeMsg;
import cn.shop.cloud.web.util.IdGenerateUtil;
import cn.shop.cloud.web.vo.SeckillGoodVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @program: work
 * @ClassName OrderInfoServiceImpl
 * @description:
 * @author: 李尧坤
 * @create: 2025−01-09 19:44
 * @Version 1.0
 **/
@Service
public class OrderInfoServiceImpl  implements IOrderInfoService {

    //以前这里都注入 mapper
    @Autowired
    private ISeckillGoodService seckillGoodService;

    @Autowired
    private ISeckillOrderService seckillOrderService;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    /**
     * 三个操作都在这里面写
     * @param userId
     * @param seckillId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String doSeckill(Long userId, Long seckillId) {
        //秒杀
        /*
           秒杀动作的原子性事务中完成 !!!
             1.秒杀商品表 t_seckill_goods 库存要 -1
             2.创建商品订单表  t_order_info  [用于后续的支付  发货 ]
             3.创建秒杀订单 t_seckill_order  用户判断用户是否存在重复抢购

         */
        //库存-1
        seckillGoodService.decrStock(seckillId);

        String orderNo = this.getOrderInfo(userId, seckillId);
        //创建订单
        seckillOrderService.createSeckillOrder(userId,seckillId,orderNo);


        return orderNo;
    }

    @Override
    public OrderInfo find(String orderNo) {
        return orderInfoMapper.find(orderNo);
    }

    @Override
    public void cancelOrder(Long seckillId, String orderNo) {

        //查看一下订单的状态
        int status=  orderInfoMapper.queryStatusByOrderNo(orderNo);

        if(status==0){
            throw  new BusinessException(SeckillCodeMsg.CANCEL_ORDER_ERROR);
        }
        //进行库存回补了
        seckillGoodService.incrStock(seckillId);
        seckillGoodService.syncRedisStcokCount(seckillId);
    }

    private  String getOrderInfo(Long userId, Long seckillId) {
        OrderInfo oi = new OrderInfo();
        //获取商品对象
        SeckillGoodVo seckillGoodVo = seckillGoodService.find(seckillId);
        oi.setCreateDate(new Date());//订单创建时间
        oi.setDeliveryAddrId(null);
        oi.setGoodCount(1);
        //购买的什么商品
        oi.setGoodId(seckillGoodVo.getGoodId());
        oi.setGoodImg(seckillGoodVo.getGoodImg());
        oi.setGoodName(seckillGoodVo.getGoodName());
        oi.setGoodPrice(seckillGoodVo.getGoodPrice());
        //秒杀价
        oi.setSeckillPrice(seckillGoodVo.getSeckillPrice());
        oi.setUserId(userId);//订单所有者
        //订单编号
        oi.setOrderNo(IdGenerateUtil.get().nextId()+"");
        //存入数据库
        orderInfoMapper.insert(oi);

        //订单编号
        return oi.getOrderNo();
    }
}

